package com.rzico.sbl.ui.report.invite

import androidx.core.view.isVisible
import com.jeremyliao.liveeventbus.utils.observe
import com.rzico.sbl.R
import com.rzico.sbl.databinding.ActivityReportInviteBinding
import com.rzico.sbl.model.BarrelData
import com.rzico.sbl.viewmodel.ReportInviteViewModel
import com.xinnuo.common.extend.*
import com.xinnuo.common.helper.bottomHide
import com.xinnuo.common.helper.bottomShow
import com.xinnuo.common.helper.getTimeString
import com.xinnuo.common.listener.onTabSelectedListener
import com.xinnuo.common.decoration.SpaceTBDecoration
import com.xinnuo.common_ui.base.BaseActivity
import com.xinnuo.common_ui.base.addItems
import com.xinnuo.common_ui.base.loadLinear
import com.xinnuo.common_ui.utils.addOnScrollChanged
import com.xinnuo.slimadapter.SlimAdapter

class ReportInviteActivity : BaseActivity(R.layout.activity_report_invite) {

    private val mBinding by lazy { ActivityReportInviteBinding.bind(getRootView()) }

    private var mStatus = ""

    override fun getViewModel() = get<ReportInviteViewModel>()

    override fun initData() {
        initTitle(title = "入空桶列表")

        initLayout()
        initTabLayout()

        observe<String>("inviteList") { updateList() }
    }

    private fun initLayout() {
        mBinding
            .listLayout
            .apply {
                emptyLayout.also {
                    it.emptyHint.text = "暂无入库单信息\n请点击添加入库生成入库单！"
                }

                recycleList.also { view ->
                    view.loadLinear(
                        refresh = swipeRefresh,
                        onRefresh = { getData(0) },
                        onLoad = {
                            if (!isLoadingMore) {
                                isLoadingMore = true
                                getData(pageNum)
                            }
                        }
                    )

                    view.addItemDecoration(SpaceTBDecoration(hasHeader = false, bottom = 65f))
                    view.addOnScrollChanged(
                        onShow = { mBinding.inviteBottom.bottomShow() },
                        onHide = { mBinding.inviteBottom.bottomHide(52f) }
                    )

                    mAdapter = SlimAdapter.creator()
                        .register<BarrelData>(R.layout.item_report_invite) { jector, bean, _ ->
                            jector.apply {
                                text(R.id.item_invite_time, bean.createDate.getTimeString())
                                text(R.id.item_invite_title, bean.name)
                                text(R.id.item_invite_count, bean.subQuantity)
                                text(R.id.item_invite_name, bean.operator.orEmpty())
                                text(R.id.item_invite_memo, bean.memo.orEmpty())

                                visibility(
                                    R.id.item_invite_memo_ll,
                                    goneValue { bean.memo.isNullOrEmpty() }
                                )

                                clicked {
                                    when (mStatus) {
                                        "credit" ->
                                            startActivityEx<ReportInviteEditActivity>(
                                                "inviteId" to bean.id,
                                                "shopId" to bean.enterpriseId,
                                                "shopName" to bean.name
                                            )
                                        "debit" ->
                                            startActivityEx<ReportInviteDetailActivity>(
                                                "inviteId" to bean.id,
                                                "shopId" to bean.enterpriseId,
                                                "shopName" to bean.name
                                            )
                                    }
                                }
                            }
                        }
                        .attachTo(view)

                }
            }
    }

    /**
     * 初始化 TabLayout
     */
    private fun initTabLayout() {
        mBinding
            .inviteTab
            .apply {
                onTabSelectedListener {
                    onTabSelected {
                        it?.let {
                            mStatus = when (it.position) {
                                0 -> "credit"
                                1 -> "debit"
                                else -> ""
                            }

                            mBinding
                                .listLayout
                                .emptyLayout
                                .emptyHint
                                .text = when (mStatus) {
                                "credit" -> "暂无入库单信息\n请点击添加入库生成入库单！"
                                else -> "暂无相关入库单信息！"
                            }

                            mBinding
                                .inviteBottom
                                .isVisible = mStatus == "credit"

                            getViewModel().cancelRequest()
                            getViewModel().delay { updateList() }
                        }
                    }
                }

                addTab(this.newTab().setText("待出单"), true)
                addTab(this.newTab().setText("已出单"), false)
            }
    }

    override fun initListener() {
        mBinding.apply {
            inviteAdd.oneClick {
                startActivityEx<ReportInviteStationActivity>()
            }
        }
    }

    /**
     * 库管入库空桶单列表
     */
    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel()
            .inviteList(
                page = index,
                status = mStatus,
                onSuccess = {
                    mList.apply {
                        if (index == 0) {
                            clear()
                            pageNum = 0
                        }
                        addItems(it)
                        it.notEmpty { pageNum++ }
                    }

                    mAdapter.setDataList(mList)
                },
                onFinally = {
                    isLoadingMore = false

                    mBinding
                        .listLayout
                        .apply {
                            swipeRefresh.isRefreshing = false

                            emptyLayout
                                .emptyView
                                .isVisible = mList.isEmpty()
                        }
                }
            )
    }

    /**
     * 刷新列表数据
     */
    private fun updateList() {
        mBinding
            .listLayout
            .apply {
                swipeRefresh.isRefreshing = true

                emptyLayout
                    .emptyView
                    .gone()
            }

        if (mList.isNotEmpty()) {
            mList.clear()
            mAdapter.notifyDataSetChanged()
        }

        getData(0)
    }

}